package cn.ghf.org.service.impl;

import cn.ghf.basic.service.impl.BaseServiceImpl;
import cn.ghf.basic.util.PageList;
import cn.ghf.org.domain.Department;
import cn.ghf.org.mapper.DepartmentMapper;
import cn.ghf.org.query.DepartmentQuery;
import cn.ghf.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper mapper;

    @Override
    @Transactional
    public void add(Department department) {
        mapper.add(department);
        this.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        //父级部门为空，
        if(department.getParentDept()==null){
            department.setDirPath("/"+department.getId());
        }else{ //父级部门不为空 获取父级的dirPath，与自己的id拼接
            Department loadById = mapper.loadById(department.getParentDept().getId());
            department.setDirPath(loadById.getDirPath()+"/"+department.getId());
        }
        mapper.update(department);
    }


    @Override
    public List<Department> getTree() {
        return mapper.getTree();
    }
}
